package com.ztmg.water.service.water.impl;

import com.ztmg.water.model.water.WaterBrand;
import com.ztmg.water.vo.water.WaterBrandVO;
import com.ztmg.water.dto.water.WaterBrandDTO;
import com.ztmg.water.dto.water.WaterBrandPageDTO;
import com.ztmg.common.util.resultUtils.JsonResult;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ztmg.common.constant.ErrorConst;
import org.springframework.beans.BeanUtils;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import com.ztmg.water.service.water.WaterBrandService;
import com.ztmg.water.mapper.water.WaterBrandMapper;import com.ztmg.water.model.water.WaterBrandExample;

@Service
public class WaterBrandServiceImpl implements WaterBrandService{

    @Resource
    private WaterBrandMapper waterBrandMapper;


    @Override
    public JsonResult selectCountWaterBrand(WaterBrandDTO dto) {
        WaterBrandExample example = new WaterBrandExample();
        WaterBrandExample.Criteria criteria =  example.createCriteria();
        long  nums =  waterBrandMapper.countByExample(example);
        return JsonResult.success(nums);
    }

    @Override
    public JsonResult create(WaterBrandDTO dto) {
        if(null != dto){
            WaterBrand bean = new WaterBrand();
            BeanUtils.copyProperties(dto,bean);
            bean.setCreateTime(new Date());
            //后续完善
            bean.setCreater("");
            waterBrandMapper.insert(bean);
            return JsonResult.success();
        }else {
            return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
        }
    }

    @Override
    public JsonResult update(WaterBrandDTO dto) {
        if(null != dto && null != dto.getId()){
            WaterBrand bean = new WaterBrand();
            BeanUtils.copyProperties(dto,bean);
            bean.setModifyTime(new Date());
            //后续完善
            bean.setModifier("");
            waterBrandMapper.updateByPrimaryKeySelective(bean);
            return JsonResult.success();
        }else {
            return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
        }
    }

    @Override
    public JsonResult<WaterBrandVO> selectById(Integer id) {
        if(null != id){
            WaterBrand bean = waterBrandMapper.selectByPrimaryKey(id);
            WaterBrandVO vo = new WaterBrandVO();
            if(null != bean){
                BeanUtils.copyProperties(bean,vo);
            }else {
                return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
            }
            return JsonResult.success(vo);
        }else{
            return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
        }
    }

    @Override
    public JsonResult<List<WaterBrandVO>> select(WaterBrandDTO dto) {
        WaterBrandExample example = new WaterBrandExample();
        WaterBrandExample.Criteria criteria =  example.createCriteria();
        /***
        * 根据具体的业务，查询条件赋值
         */
        List<WaterBrand> sysUserList = waterBrandMapper.selectByExample(example);
        List<WaterBrandVO> respDTOS = sysUserList.parallelStream().map((bean) -> {
            WaterBrandVO vo = new WaterBrandVO();
            BeanUtils.copyProperties(bean, vo);
            return vo;
        }).collect(Collectors.toList());

        return JsonResult.success(respDTOS);
    }

    @Override
    public JsonResult<PageInfo<WaterBrandVO>> selectPage(WaterBrandPageDTO dto) {
        WaterBrandExample example = new WaterBrandExample();
        WaterBrandExample.Criteria criteria =  example.createCriteria();
        /***
        * 根据具体的业务，查询条件赋值
         */
        PageHelper.startPage(dto.getPage(), dto.getLimit());
        List<WaterBrand> beanList = waterBrandMapper.selectByExample(example);
        PageInfo pageInfo = new PageInfo<>(beanList);
        List<WaterBrandVO> respDTOS = beanList.parallelStream().map((bean) -> {
            WaterBrandVO vo = new WaterBrandVO();
            BeanUtils.copyProperties(bean, vo);
            return vo;
        }).collect(Collectors.toList());
        pageInfo.setList(respDTOS);
        return JsonResult.success(pageInfo);
    }

    @Override
    public JsonResult deleteById(Integer id) {
        return JsonResult.success( waterBrandMapper.deleteByPrimaryKey(id));
    }

    @Override
    public JsonResult delete(WaterBrandDTO dto) {
        WaterBrandExample example = new WaterBrandExample();
        WaterBrandExample.Criteria criteria =  example.createCriteria();
        /***
        * 根据具体的业务，查询条件赋值
         */
        return JsonResult.success(waterBrandMapper.deleteByExample(example));
    }
}